package org.sso;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import javax.annotation.Resource;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class MemcachedClientTemplate {

	@Resource
	private  MemcachedClient memcachedClient;
	
	@Value("${memcache.exp.defalut}")
	private  String memcacheExpDefalut;
	@Value("${memcache.timeout.defalut}")
	private  String memcacheTimeoutDefalut;
	
	
	
	public MemcachedClient getMemcachedClient() {
		return memcachedClient;
	}

	public void setMemcachedClient(MemcachedClient memcachedClient) {
		this.memcachedClient = memcachedClient;
	}

	/**
	 * 
	 * @param key     键
	 * @param value	     值
	 * @param exp     过期时间单位是秒
	 * @param timeout
	 */
	public  boolean setCacheValue(String key,Object value,Integer exp,Long timeout){
		try {
			if (null==exp)
				exp = Integer.parseInt(memcacheExpDefalut);
			if (null==timeout)
				timeout = Long.parseLong(memcacheTimeoutDefalut);			
			return memcachedClient.set(key, exp, value, timeout);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 
	 * @param key     键
	 * @param value	     值
	 * @param exp     过期时间单位是秒
	 * @param timeout
	 */
	public  boolean setCacheValue(String key,Object value,Integer exp){
		try {
			if (null==exp)
				exp = Integer.parseInt(memcacheExpDefalut);
			return memcachedClient.set(key, exp, value);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 
	 * @param key 健
	 * @param value  值
	 * @param exp   过期时间
	 * @return
	 */
	public  boolean replaceKey(String key,Object value,Integer exp){
		try {
			if (null==exp)
				exp = Integer.parseInt(memcacheExpDefalut);
			return memcachedClient.replace(key, exp, value);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 
	 * @param key   健
	 * @param value 值
	 * @param exp   过期时间
	 * @param timeout
	 * @return
	 */
	public  boolean replaceKey(String key,Object value,Integer exp,Long timeout){		
		try {
			if (null==exp)
				exp = Integer.parseInt(memcacheExpDefalut);
			if (null==timeout)
				timeout = Long.parseLong(memcacheTimeoutDefalut);
			return memcachedClient.replace(key, exp, value, timeout);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public  boolean addKey(String key,Object value,Integer exp,Long timeout){
		try {
			if (null==exp)
				exp = Integer.parseInt(memcacheExpDefalut);
			if (null==timeout)
				timeout = Long.parseLong(memcacheTimeoutDefalut);
			return memcachedClient.add(key, exp, value, timeout);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public  boolean addKey(String key,Object value,Integer exp){
		try {
			if (null==exp)
				exp = Integer.parseInt(memcacheExpDefalut);
			return memcachedClient.add(key, exp, value);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public  boolean deleteByKey(String key){
		try {
			return memcachedClient.delete(key);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 
	 * @param key    健
	 * @param opTimeout 操作超时
	 * @return
	 */
	public  boolean deleteByKey(String key,Long opTimeout){
		try {
			return memcachedClient.delete(key,opTimeout);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	
	public Object getObjectByKey(String key ,Long timeout){
		try {
			if (null==timeout)
				timeout = Long.parseLong(memcacheTimeoutDefalut);
			return memcachedClient.get(key, timeout);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Map<String,Object> getObjectByKeyCollections(Collection<String> keyCollections,Long timeout){
		try {
			if (null==timeout)
				timeout = Long.parseLong(memcacheTimeoutDefalut);
			return memcachedClient.get(keyCollections, timeout);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return null;
	}
	

}
